package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import dao.RowMapper;
import dao.StreetDao;
import entity.Area;
import entity.Street;

public class StreetDaoImpl implements StreetDao {
	private JDBCTemplate jdbc = new JDBCTemplate();
	
	private class RowMapperImpl implements RowMapper<Street>{

		@Override
		public Street rowMapper(ResultSet rs) throws SQLException {
			return new Street(rs.getInt("strId"), new Area(rs.getInt("areaId"), 
								rs.getString("areaName"), null),
								rs.getString("streetName"));
		}
		
	}
	
	@Override
	public List<Street> queryAllStreet() {
		String sql = "SELECT street.*,areaName FROM street"+
					" INNER JOIN `area` ON `area`.`areaId`=street.`areaId`";
		return jdbc.queryAll(sql, new RowMapperImpl());
	}

	@Override
	public Street queryStreeByStreetId(int streetId) {
		String sql = "SELECT street.*,areaName FROM street"+
				" INNER JOIN `area` ON `area`.`areaId`=street.`areaId`"+
				" where strId=?";
		return jdbc.queryOne(sql, new RowMapperImpl(), streetId);
	}

	@Override
	public List<Street> queryStreetByAreaId(int areaId) {
		String sql = "SELECT street.*,areaName FROM street"+
				" INNER JOIN `area` ON `area`.`areaId`=street.`areaId`"+
				" where street.areaId=?";
		return jdbc.queryAll(sql, new RowMapperImpl(), areaId);
	}

}
